<?php

    $DS_HOST = "127.0.0.1:8088";
    $MYSQL_HOST = "127.0.0.1";
    $MYSQL_USER = "root";
    $MYSQL_DB = "db_draco";
    $MYSQL_PASS = "";
    $USER_TABLE = "user";
    
    session_start();
    date_default_timezone_set('Asia/Shanghai');
    
    require_once("db.php");

    function oplog($ownerid, $userid, $username, $appname, $dirname, $client, $sourcefile, $targetfile, $content, $optime=NULL)
    {
        global $db;
        if ($optime === NULL)
        {
            $optime = time();
            $sql = "insert into op_log (ownerid, userid, username, appname, dirname, client, sourcefile, targetfile, content) values ".
                                    "(\"$ownerid\", \"$userid\", \"$username\", \"$appname\", \"$dirname\", \"$client\", \"$sourcefile\", \"$targetfile\", \"$content\");";
        }
        else
        {
            $sql = "insert into op_log (ownerid, userid, username, appname, dirname, client, sourcefile, targetfile, content, ctime) values ".
                                    "(\"$ownerid\", \"$userid\", \"$username\", \"$appname\", \"$dirname\", \"$client\", \"$sourcefile\", \"$targetfile\", \"$content\", \"$optime\");";
        }
        
        $ret = $db->query($sql);
        if (!$ret)
        {
            echo $db->error();
        }
        return $ret;
    }
    
    function savedirid($ownerid, $dirid, $dirname)
    {
        global $db;
        
        $sql = "insert into dir_cache (userid, dirid, dirname) values (\"$ownerid\", \"$dirid\", \"$dirname\");";
        return $db->query($sql);
    }
    
    function getdirname($ownerid, $dirid)
    {
        global $db;
        if ($dirid == 0)
        {
            return "/";
        }
        $sql = "select dirname from dir_cache where userid=$ownerid and dirid=$dirid;";
        $ret = $db->fetch_first($sql);
        return $ret["dirname"];
    }
    
    function checkgroup($userid, $groupid)
    {
        global $db;
        
        // check groupid
        $sql = "select count(*) from users where userid=$groupid and typeid=3;";
        $result = $db->result_first($sql);

        if ($result == 0)
        {
            return false;
        }
        // check relation
        $sql = "select count(*) from group_rel where userid=$userid and groupid=$groupid;";
        $result = $db->result_first($sql);
        if ($result == 0)
        {
            return false;
        }
        return true;
    }
?>
